#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import click


def dofile(path, licenseText):
    lines = []

    with open(path, "rt") as f:
        lines = f.readlines()
        f.close()

    if lines[1].find("coding: "):
        pos = 2
    else:
        pos = 1

    out = []
    out.extend(lines[:pos])
    out.extend(["# " + l for l in licenseText])
    out.extend(lines[pos:])

    with open(path, "wt") as f:
        f.writelines(out)
        f.close()


@click.command()
@click.option('--license', '-l', help="License file.")
@click.argument('files', nargs=-1)
def main(license, files):
    lfile = license or "~/LICENSE.txt"

    lines = []
    with open(lfile, "rt") as f:
        lines.extend(f.readlines())

    for f in files:
        dofile(f, lines)

if __name__ == "__main__":
    sys.exit(main())

# vim: sw=4 ts=4 sts=4 ai et
